package com.yitiao.consumer;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class ConsumerStarter {

    @Resource
    private DynamicTopicConsumer dynamicTopicConsumer;

    @Resource
    private ConsumerQueueConfig consumerQueueList;

    @PostConstruct
    public void consumeMessage() {
        List<String> consumerQueues = consumerQueueList.getQueues();
        if (consumerQueueList.getEnabled() && CollectionUtil.isNotEmpty(consumerQueues)) {
            consumerQueues.forEach(k->{
                String[] split = k.split(",");
                if (split.length==3){
                    dynamicTopicConsumer.consume(split[0], split[1], split[2]);
                }else {
                    throw new RuntimeException("please check the mq-consumer configuration["+k+"],make it conform to this format:[- exchange,routingKey,queue]");
                }
            });
        }
    }
}
